<template>
  <PageContainer :transparent="true">
    <Header title="管理员邀请" />
    <template>
      <!-- 未操作 -->
      <view
        v-if="pageType === 'normal'"
        class="hb-flex hb-flex-col hb-items-center hb-py-10 hb-px-4"
      >
        <TmImage
          class="hb-mb-4"
          :src="invitationCodeInfo?.iconFileUrl"
          error-img-type="person"
          :width="160"
          :height="160"
          :round="25"
        />
        <TmText _class="hb-mb-4 hb-font-bold" label="邀请管理员" :font-size="48" />
        <TmText
          _class="hb-mb-4"
          :label="`${invitationCodeInfo?.invitingUserName}邀请您一起管理智能助手：`"
          :font-size="32"
        />
        <TmText :label="`“${invitationCodeInfo?.botName}”`" color="primary" :font-size="32" />
        <BottomSubmit
          :show-bg-color="false"
          :hide-cancel-btn="false"
          :is-station="false"
          :bottom="30"
          cancel-btn-label="拒绝邀请"
          submit-btn-label="同意邀请"
          @submit="handleConfirm"
          @cancel="handleCancel"
        />
      </view>
      <!-- 同意 -->
      <view v-else-if="pageType === 'agree'" class="hb-flex hb-flex-col hb-items-center hb-py-10">
        <TmIcon
          _class="hb-mb-4"
          name="ali-icon icon-danxuan_xuanzhong"
          :font-size="128"
          color="primary"
        />
        <TmText _class="hb-mb-4 hb-font-bold" label="已接受邀请" :font-size="48" />
        <TmText _class="hb-text-center" label="可开始管理智能助手" :font-size="32" />
        <BottomSubmit
          :show-bg-color="false"
          :is-station="false"
          :bottom="30"
          submit-btn-label="回到首页"
          @submit="handleBackHome"
        />
      </view>
      <!-- 拒绝 -->
      <view v-else-if="pageType === 'reject'" class="hb-flex hb-flex-col hb-items-center hb-py-10">
        <TmIcon
          _class="hb-mb-4"
          name="ali-icon icon-close_circle_fill"
          :font-size="128"
          color="#EA5E0E"
        />
        <TmText _class="hb-mb-4 hb-font-bold" label="已拒绝邀请" :font-size="48" />
        <TmText _class="hb-text-center" label="您已拒绝成为智能助手管理员" :font-size="32" />
        <BottomSubmit
          :show-bg-color="false"
          :is-station="false"
          :bottom="30"
          submit-btn-label="回到首页"
          @submit="handleBackHome"
        />
      </view>
      <!-- 失效 -->
      <view v-else-if="pageType === 'invalid'" class="hb-flex hb-flex-col hb-items-center hb-py-10">
        <TmIcon _class="hb-mb-4" name="ali-icon icon-yichang2" :font-size="128" color="#E29942" />
        <TmText _class="hb-mb-4 hb-font-bold" label="链接已失效" :font-size="48" />
        <TmText
          _class="hb-text-center"
          label="当前链接已失效，请让负责人重新邀请"
          :font-size="32"
        />
        <BottomSubmit
          :show-bg-color="false"
          :is-station="false"
          :bottom="30"
          submit-btn-label="回到首页"
          @submit="handleBackHome"
        />
      </view>
    </template>
  </PageContainer>
</template>

<script lang="ts" setup>
  /* assistantInvite 管理员邀请 */
  import { assistantInvitationCodeInfo, handleAssistantInvitation } from '@/api/aiSettings';
  import { AssistantInvitationCodeInfoResult } from '@/api/aiSettings/types';
  import { TabbarPageEnum } from '@/enums/PageEnum';
  import { InvitationStatusEnum } from '@/enums/PublicEnum';
  import { useUserStore } from '@/store/modules/user';

  const pageType = ref<'normal' | 'agree' | 'reject' | 'invalid'>();
  const invitationCodeInfo = ref<AssistantInvitationCodeInfoResult>();

  customOnload<'assistantInvite'>((op) => {
    assistantInvitationCodeInfo(op.invitationCode).then((res) => {
      if (res.status === InvitationStatusEnum.EFFECTIVE) {
        pageType.value = 'normal';
      } else if (res.status === InvitationStatusEnum.AGREED) {
        pageType.value = 'agree';
      } else if (res.status === InvitationStatusEnum.REJECTED) {
        pageType.value = 'reject';
      } else if (res.status === InvitationStatusEnum.INVALID) {
        pageType.value = 'invalid';
      }
      invitationCodeInfo.value = res;
    });
  });

  const handleConfirm = () => {
    handleAssistantInvitation({
      code: invitationCodeInfo.value!.code,
      status: InvitationStatusEnum.AGREED,
    }).then(() => {
      pageType.value = 'agree';
      useUserStore().getUserInfo();
    });
  };

  const handleCancel = () => {
    handleAssistantInvitation({
      code: invitationCodeInfo.value!.code,
      status: InvitationStatusEnum.REJECTED,
    }).then(() => {
      pageType.value = 'reject';
    });
  };

  const handleBackHome = () => {
    switchTab(TabbarPageEnum.BASE_HOME);
  };
</script>

<style lang="scss" scoped></style>
